package com.taptech.doufu.presenter.home;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.bean.base.BaseResponseBean;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.util.GsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 对话小说轮播图
 * Created by Wyatt on 2017/10/13/013.
 */

public class ChoiceBannerPresenter {

    public interface OnHomeBannerView{
        void onBannerNext(List<DFHomeBannerBean> list, boolean isCache);
    }

    private OnHomeBannerView onHomeBannerView;
    private Context mContext;

    public ChoiceBannerPresenter(Context context,OnHomeBannerView onHomeBannerView){
        this.onHomeBannerView = onHomeBannerView;
        mContext = context;
    }


    public void loadBannerCache() {
        Observable.create(new Observable.OnSubscribe<List<DFHomeBannerBean>>() {
                    @Override
                    public void call(Subscriber<? super List<DFHomeBannerBean>> subscriber) {
                        List<DFHomeBannerBean> list = null;
                        try {
                            JSONObject json = CacheUtil.readJsonDataFromCache(new File(CommmonImp.Home_novel_choice_banner));
                            list = GsonUtil.jsonToList(json.getJSONArray("data").toString(), DFHomeBannerBean[].class);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        subscriber.onNext(list);
                    }
                })
                .compose(RxJavaHelper.<List<DFHomeBannerBean>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<List<DFHomeBannerBean>>(){
                    @Override
                    public void onNext(List<DFHomeBannerBean> list) {
                        super.onNext(list);
                        if (onHomeBannerView != null && list!=null) {
                            onHomeBannerView.onBannerNext(list,true);
                        }
                    }
                });
    }



    /**
     * 获取轮播图数据
     */
    public void getHomeNovelBanner(boolean isUseCache) {
        ApiClient.getInstance().setUseCache(isUseCache).getService().getChoiceBanner()
                .map(new Func1<BaseResponseBean<List<DFHomeBannerBean>>, BaseResponseBean<List<DFHomeBannerBean>>>() {
                    @Override
                    public BaseResponseBean<List<DFHomeBannerBean>> call(BaseResponseBean<List<DFHomeBannerBean>> listBaseResponseBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(listBaseResponseBean),new File(CommmonImp.Home_novel_choice_banner));
                        return listBaseResponseBean;
                    }
                })
                .compose(RxJavaHelper.<BaseResponseBean<List<DFHomeBannerBean>>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<BaseResponseBean<List<DFHomeBannerBean>>>() {
                    @Override
                    public void onNext(BaseResponseBean<List<DFHomeBannerBean>> novelBanner) {
                        super.onNext(novelBanner);
                        if (onHomeBannerView != null && novelBanner!=null) {
                            onHomeBannerView.onBannerNext(novelBanner.getData(),false);
                        }
                    }
                });
    }

}
